﻿function BuildFrom(buildtype) {
  this.rules = {} /// 验证对象
  this.formData = {} /// fromData 对象
  this.methodsfunction = {} /// 方法集合
  this.watchfunction = {} /// 监控方法
  this.watchfunctionContext = {} /// 监控方法
  this.watchdetailfiled = {} /// 为了明细列字段监控设定的
  this.loaddatabeforecode = '' /// 加载之前事件
  this.loaddataaftercode = '' /// 数据加载之后事件
  this.beforesubmitcode = '' /// 提交前事件
  this.griddetail = {}
  this.vuetemp = {
    data() {
      return {}
    },
    methods: {}
  }
  this.controlData = {} /// 控件参数或者加载对象
  this.adddataCode = [] /// /新增时 需要执行的代码
  this.rowcontrolstr =
    'tree,navmenu,blank,partlayout,dynamiclayout,calendar,imgage,upFilesComp,upImgComp,upManyImgComp' /// 占整行的控件
  this.dynamicheight = 'partlayout,tree,calendar,imgage,upFilesComp,' /// 由内部控件撑高度的控件
  this.minheight = 'partlayout,dynamiclayout,'
  this.initfunstr = '' /// 初始化加载js方法
  this.buildtype = buildtype /// 生成类型
  this.fromTableName = '' /// 主表名称
  this.swaggerconfig = getStorageValue('swaggerconfig') /// swagger配置信息
  this.defaultprops = ['businessid', 'IsPower', 'fromstatus', 'fromid', '$user']
  this.casestring = {
    zhldialog: 'zhlDialog',
    editor: 'Editor',
    showsizechange: 'showSizeChange'
  }
  this.modelfiled = {}
  this.readDataPermission = {}
  this.writeDataPermission = {}
  this.defaultfromInfo = {}
  function getStorageValue(key) {
    let strStorage = localStorage.getItem(key)
    if (strStorage == null) {
      strStorage = sessionStorage.getItem(key)
    }
    if (strStorage != null) {
      const obj = JSON.parse(strStorage)
      let content = {}
      if (obj.dataType === 'string') {
        content = obj.content
      } else if (obj.dataType === 'number') {
        content = Number(obj.content)
      } else if (obj.dataType === 'boolean') {
        /* eslint-disable */
        content = eval(obj.content)
      } else if (obj.dataType === 'object') {
        content = obj.content
      }
      return content
    }
  }
}

BuildFrom.prototype.buildpubish = function(frominfo, jscode, defaultfromInfo) {
  this.defaultfromInfo = defaultfromInfo
  this.fromTableName = frominfo.fromTableName
  var isError = this.clearInit(jscode)
  if (isError == false) {
    return { code: -1, msg: '编写js代码语法有错误！请检查！' }
  }
  var htmlcode = $('#miantmpl')
    .tmpl(frominfo)
    .prop('outerHTML')
  ////将有大小写的标签进行转换一下
  for (var key in this.casestring) {
    var reg = new RegExp(key, 'g')
    htmlcode = htmlcode.replace(reg, this.casestring[key])
  }
  var jscode = this.bulidjscode(frominfo)
  var buildObject = {
    htmlcode: htmlcode,
    jscode: 'export default ' + jscode,
    csscode: this.getcss()
  }
  return buildObject
}

BuildFrom.prototype.clearInit = function(jscode) {
  this.rules = {} /// 验证对象
  this.formData = {} /// fromData 对象
  this.methodsfunction = {} /// 事件集合对象
  this.adddataCode = []
  this.modelfiled = {}
  this.readDataPermission = {}
  this.writeDataPermission = {}
  this.vuetemp = {
    data() {
      return {}
    },
    methods: {},
    watch: {}
  }
  this.controlData = {}
  this.initfunstr = ''
  this.watchfunctionContext = {}
  this.watchdetailfiled = {}
  this.watchfunction = {}
  if (jscode != undefined && jscode != '') {
    try {
      this.vuetemp = eval('(' + jscode.replace('export default', '') + ')')
    } catch {
      return false
    }
    if (this.vuetemp.methods != undefined) {
      for (let key in this.vuetemp.methods) {
        let prefix = key.substr(key.length - 4, key.length)
        if (prefix !== '_sys') {
          this.methodsfunction[key] = this.vuetemp.methods[key]
        }
      }
    }
    if (this.vuetemp.watch != undefined) {
      // this.watchfunction = Object.assign(this.vuetemp.watch)
    } else {
      this.vuetemp.watch = {}
    }
  }
  return true
}
BuildFrom.prototype.bulidjscode = function(frominfo) {
  var jscode = ''
  var newjsdata = Object.assign(this.vuetemp.data())
  newjsdata['formData'] = this.formData
  newjsdata['formData']['$tableNewId'] = ''
  newjsdata['formData']['openid'] = ''
  newjsdata['formData']['auditflag'] = ''
  newjsdata['formData']['isimport'] = ''
  newjsdata['formData']['auditremark'] = ''
  newjsdata['deviceMode'] = 'Mobile'
  newjsdata['fromdialog'] = {
    fromid: '',
    selectId: '',
    loaddata: ''
  }
  newjsdata['rules'] = this.rules
  newjsdata['controlData'] = this.controlData
  newjsdata['selectindex'] = -1
  newjsdata['isedit'] = false
  newjsdata['selectproperty'] = ''
  newjsdata['modelfiled'] = this.modelfiled
  newjsdata['watchdetailfiled'] = this.watchdetailfiled
  newjsdata['readDataPermission'] = this.readDataPermission
  newjsdata['writeDataPermission'] = this.writeDataPermission
  if (this.vuetemp['props'] === undefined) {
    this.vuetemp['props'] = this.defaultprops
  } else {
    this.defaultprops.forEach(ee => {
      if (this.vuetemp['props'].indexOf(ee) < 0) {
        this.vuetemp['props'].push(ee)
      }
    })
  }
  var methodseventfunction = ''
  this.buildformulaCode(frominfo.logicList)
  this.buildCommonJsCode()
  function bulidobject(obj) {
    var jscode = '{'
    for (key in obj) {
      var codestr = jscode == '{' ? '' : ','
      if (key == 'formData') {
        jscode =
          jscode + codestr + ' ' + key + ":'" + JSON.stringify(obj[key]) + "'"
      } else if (typeof obj[key] == 'function') {
        jscode = jscode + codestr + obj[key].toString()
      } else if (typeof obj[key] == 'object') {
        if (obj[key] instanceof Array) {
          jscode = jscode + codestr + key + ':' + buildArray(obj[key]) + ''
        } else {
          jscode = jscode + codestr + key + ':{' + bulidobject(obj[key]) + '}'
        }
      } else if (typeof obj[key] != 'undefined') {
        jscode =
          jscode +
          codestr +
          key +
          ":'" +
          obj[key].toString().replace(/\"/g, '\\"') +
          "'"
      }
    }
    jscode = jscode + '}'
    return jscode
  }
  function buildArray(arry) {
    var jscode = '['
    for (let idex = 0; idex < arry.length; idex++) {
      var codestr = jscode == '[' ? '' : ','
      if (typeof arry[idex] == 'object') {
        if (arry[idex] instanceof Array) {
          jscode = jscode + codestr + buildArray(arry[idex])
        } else {
          jscode = jscode + codestr + bulidobject(arry[idex])
        }
      } else {
        jscode = jscode + codestr + '"' + arry[idex] + '"'
      }
    }
    jscode = jscode + ']'
    return jscode
  }
  // this.methodsfunction["funmounted"] = `\n//系统方法页面加载方法 \nfunmounted(){${this.initfunstr}}`
  for (var key in this.methodsfunction) {
    methodseventfunction =
      methodseventfunction == ''
        ? this.methodsfunction[key].toString()
        : methodseventfunction + ',' + this.methodsfunction[key].toString()
  }
  let watchfunctioncode = ''
  for (var key in this.watchfunction) {
    let watchfunctionjscode =
      this.watchfunctionContext[key] instanceof Array
        ? this.watchfunctionContext[key].join('\n')
        : this.watchfunctionContext[key]
    watchfunctioncode =
      watchfunctioncode == ''
        ? this.watchfunction[key]
            .toString()
            .replace('$$##$$', watchfunctionjscode)
        : watchfunctioncode +
          ',' +
          this.watchfunction[key]
            .toString()
            .replace('$$##$$', watchfunctionjscode)
  }
  jscode = jscode + '{'
  for (key in this.vuetemp) {
    var codestr = jscode == '{' ? '' : ','
    if (key == 'data' && typeof this.vuetemp[key] == 'function') {
      jscode =
        jscode + codestr + ' data(){ return ' + JSON.stringify(newjsdata) + '} '
    } else if (typeof this.vuetemp[key] == 'function') {
      jscode = jscode + codestr + this.vuetemp[key].toString()
    } else if (key == 'methods' && typeof this.vuetemp[key] == 'object') {
      jscode = jscode + codestr + ' methods:{' + methodseventfunction + '} '
    } else if (key == 'watch' && typeof this.vuetemp[key] == 'object') {
      jscode = jscode + codestr + ' watch:{' + watchfunctioncode + '} '
    } else if (typeof this.vuetemp[key] == 'object') {
      if (this.vuetemp[key] instanceof Array) {
        jscode =
          jscode +
          codestr +
          ' ' +
          key +
          ':' +
          buildArray(this.vuetemp[key]) +
          ''
      } else {
        jscode =
          jscode +
          codestr +
          ' ' +
          key +
          ':{' +
          bulidobject(this.vuetemp[key]) +
          '} '
      }
    } else if (typeof this.vuetemp[key] != 'undefined') {
      jscode =
        jscode +
        codestr +
        ' ' +
        key +
        ":'" +
        this.vuetemp[key].toString().replace(/\"/g, '\\"') +
        "'"
    }
  }
  if (this.vuetemp['mounted'] == undefined || this.vuetemp['mounted'] == null) {
    jscode = jscode + `,mounted(){this.funmounted_sys();}`
  }
  jscode = jscode + '}'
  return jscode
}
BuildFrom.prototype.buildCommonJsCode = function() {
  switch (this.buildtype) {
    case 'buildpublish':
      this.buildCommonBuildFromTmpe()
      break
    case 'buildpublishAntDesign':
      this.buildCommonBuildFromTmpe()
      break
    case 'buildquestionnaire':
      this.buildCommonBuildFromTmpe()
      break
    case 'buildscreenanalyse':
      break
    case 'buildpublish':
      break
  }
}
BuildFrom.prototype.buildformulaCode = function(logicList) {
  if (logicList !== undefined) {
    logicList.forEach(ee => {
      if (ee.relationfield.indexOf('loaddatabefore') >= 0) {
        this.loaddatabeforecode = 'this.loaddatabefore_sys();'
        this.buildformulaItemCode('loaddatabefore_sys', ee.jscode, '')
      } else if (ee.relationfield.indexOf('loaddataafter') >= 0) {
        this.loaddataaftercode = 'this.loaddataafter_sys();'
        this.buildformulaItemCode('loaddataafter_sys', ee.jscode, '')
      } else if (ee.relationfield.indexOf('beforesubmit') >= 0) {
        this.beforesubmitcode =
          'let beforesubbool= this.beforesubmit_sys(); if(beforesubbool===false){return false;}'
        this.buildformulaItemCode('beforesubmit_sys', ee.jscode, '')
      } else if (ee.relationfield.indexOf('.') < 0) {
        ////然后还剩逻辑设置代码未生成。
        this.watchfunction[
          `formData.${ee.relationfield}`
        ] = `'formData.${ee.relationfield}'(){$$##$$}`
        let key = `formData.${ee.relationfield}`
        if (this.watchfunctionContext[key] !== undefined) {
          this.watchfunctionContext[key] = this.watchfunctionContext[
            key
          ].concat(`   this.${ee.logickey + '_sys'}();`)
        } else {
          this.watchfunctionContext[key] = `   this.${ee.logickey + '_sys'}();`
        }
        this.buildformulaItemCode(ee.logickey + '_sys', ee.jscode)
      } else {
        let filed = ee.relationfield.split('.')
        this.watchdetailfiled[filed[1]] = ''
        this.watchfunction[
          `watchdetailfiled.${filed[1]}`
        ] = `'watchdetailfiled.${filed[1]}'(){ $$##$$}`
        this.watchfunction[`formData.${filed[0]}`] = `'formData.${filed[0]}':{
          deep:true,
          handler:function(newVal){
            if(this.selectproperty!==undefined&&this.selectindex!==undefined){
            if(newVal[this.selectindex]!==undefined&&newVal[this.selectindex][this.selectproperty]!==undefined){$$##$$}
            }
          }
        }`
        let key = `formData.${filed[0]}`
        if (this.watchfunctionContext[key] !== undefined) {
          this.watchfunctionContext[key] = this.watchfunctionContext[
            key
          ].concat(
            `  this.watchdetailfiled.${filed[1]}=newVal[this.selectindex].${filed[1]};`
          )
        } else {
          this.watchfunctionContext[
            key
          ] = `  this.watchdetailfiled.${filed[1]}=newVal[this.selectindex].${filed[1]};`
        }
        key = `watchdetailfiled.${filed[1]}`
        ///监控列变化时 调用逻辑配置
        if (this.watchfunctionContext[key] !== undefined) {
          this.watchfunctionContext[key] = this.watchfunctionContext[
            key
          ].concat(
            `   this.${ee.logickey + '_sys'}(this.formData.${
              filed[0]
            }[this.selectindex]);`
          )
        } else {
          this.watchfunctionContext[key] = `   this.${ee.logickey +
            '_sys'}(this.formData.${filed[0]}[this.selectindex]);`
        }
        this.buildformulaItemCode(ee.logickey + '_sys', ee.jscode, filed[0])
      }
    })
  }
}
////生成具体的逻辑代码
BuildFrom.prototype.buildformulaItemCode = function(
  funname,
  htmlcode,
  detailfiled
) {
  let jscode = htmlcode
  const formulahtml = $('<div></div>').append(htmlcode)
  const spanparam = formulahtml.find('[val]')
  for (let i = 0; i < spanparam.length; i++) {
    const ee = spanparam[i]
    let formulaparam = {
      formulname: $(ee).attr('val'),
      vartype: $(ee).attr('vartype'),
      variabletype: $(ee).attr('variabletype')
    }
    switch (formulaparam.vartype) {
      case '1':
        if (formulaparam.formulname.indexOf('.') < 0) {
          jscode = jscode.replace(
            $(ee).prop('outerHTML'),
            'this.formData.' + formulaparam.formulname
          )
        } else {
          let filed = formulaparam.formulname.split('.')
          if (detailfiled === filed[0] && formulaparam.variabletype !== '1') {
            jscode = jscode.replace(
              $(ee).prop('outerHTML'),
              `datarow.${filed[1]}`
            )
          } else {
            jscode = jscode.replace(
              $(ee).prop('outerHTML'),
              `this.formData.${filed[0]},'${filed[1]}'`
            )
          }
        }
        break
      case '2':
        jscode = jscode.replace(
          $(ee).prop('outerHTML'),
          'this.' + formulaparam.formulname
        )
        break
      case '3':
        jscode = jscode.replace(
          $(ee).prop('outerHTML'),
          'this.$route.query.' + formulaparam.formulname
        )
        break
    }
  }
  this.methodsfunction[funname] = `${funname}(datarow){
    try{
     ${jscode
       .replace(/<\/?.+?>/g, '')
       .replaceAll('；', ';')
       .replaceAll('，', '，')
       .replaceAll('“', '"')
       .replaceAll('’', "'")
       .replaceAll('&nbsp;', '')
       .replaceAll('&amp;', '&')}

    }catch (ex) {
      this.$message.error('函数${funname}逻辑代码报错！'+ex)
      return false
    }
    return true
  }`
}
////生成通用方法
BuildFrom.prototype.buildCommonBuildFromTmpe = function() {
  this.methodsfunction[
    'fromdialogclose_sys'
  ] = `\n //系统方法 关闭弹出层的方法  \n fromdialogclose_sys(issave){
  this.$refs.reffromdialog.clasedialog();
    if (issave&& this.fromdialog.loaddata!==undefined &&  typeof(this.fromdialog.loaddata)==='function') {
      this.fromdialog.loaddata()
    }   }`

  this.methodsfunction[
    'addGridlist_sys'
  ] = `\n //系统方法 列表新增方法  \n addGridlist_sys(controlId,fromid){
    if(fromid===undefined||fromid===''||fromid===null){
      this.$message.error("找不到表单信息！");
    }
    this.fromdialog.loaddata=this[ 'load' + controlId + '_sys']
    this.fromdialog.selectId=''
    this.$refs.reffromdialog.opendialog(fromid,'')
     }`
  this.methodsfunction[
    'updateGridlist_sys'
  ] = `\n //系统方法 列表新增方法  \n updateGridlist_sys(controlId,fromid,rowdata){
      if(fromid===undefined||fromid===''||fromid===null){
        this.$message.error("找不到表单信息！");
      }
      this.fromdialog.loaddata=this[ 'load' + controlId + '_sys']
      this.$refs.reffromdialog.opendialog(fromid,rowdata.fid)
       }`
  this.methodsfunction[
    'delGridlist_sys'
  ] = `\n //系统方法 删除行方法 \n delGridlist_sys(controlId,rowdata){
        this.$confirm('确定删除，删除后将无法恢复, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$apiComm(this.controlData[controlId].tablename+".deleteInfo", {body:{fid:rowdata.fid}}).then(res => {
              if (res.code != 0) {
                  this.$message.error(res.msg);
              }
              this[ 'load' + controlId + '_sys']()
              this.$message.success("保存成功！")
          });

        });

       }`
  this.methodsfunction[
    'submitData_sys'
  ] = `\n //系统方法 通用提交保存方法 \n  submitData_sys() {
    // console.log(JSON.stringify(this.formData))
    // return
    this.$refs['elForm'].validate(valid => {
        if (!valid) return
        
        
        const param = {
            body: { ...this.formData,$fromid:this.fromid }
        };
        ${this.beforesubmitcode}
        
        this.$apiComm("${this.fromTableName}.SaveDataInfo", param).then(res => {
            if (res.code != 0) {
                //this.$message.error(res.msg);
                swal({
                  title: "提交失败",
                  text: res.msg,
                  icon: "error",
                  button: "确认"
                })
            }
            if(typeof this.dialogclose =='function'){
              this.dialogclose(true)
            }
            //this.$message.success("保存成功！")
            localStorage.removeItem("openId")
            localStorage.removeItem("formInfo")

            if(localStorage.getItem("projectId")==109)
            {
              swal({
                title: "提示信息",
                text: "数据提交成功，请务必于2月6日扫描二维码查看报名审核结果及考试安排，考试安排不再另行通知。",
                icon: "success",
                buttons: false,
                closeOnClickOutside: false
              });
            }
            else
            {
              swal({
                title: "提示信息",
                text: "数据提交成功，请后续再次登陆该页面核实您的审核结果",
                icon: "success",
                buttons: false,
                closeOnClickOutside: false
              });
            }

        });
    })
}`
  this.methodsfunction[
    'addListViewRow_sys'
  ] = `\n //系统方法 通用添加明细行方法 \n  addListViewRow_sys(datalistname) {
    var newrowobj={}
    this.controlData[datalistname].scopedSlots.forEach(ee=>{
newrowobj[ee]="";
    })
    this.formData[datalistname].push(newrowobj);
}`
  this.methodsfunction[
    'delListViewRow_sys'
  ] = `\n //系统方法 通用删除明细方法 \n  delListViewRow_sys(datalistname,rows) {
     if(rows.fid!=undefined && rows.fid!=""){
        this.formData["del"+datalistname].push(rows.fid);
    }
    this.formData[datalistname].splice(this.formData[datalistname].indexOf(rows),1);

}`

  this.methodsfunction[
    'getTableNewId_sys'
  ] = `\n //系统方法 判断url是否传入ID，如果传入给各个控件赋值 \n  getTableNewId_sys() {
    ${this.loaddatabeforecode}

  const tablefid = this.$query("tablefid");
    let newbusinessid=this.businessid;
    if(tablefid!=null&&tablefid!=''){
        newbusinessid=tablefid;
    }
if(newbusinessid!=null&&newbusinessid!=undefined &&newbusinessid.trim()!=""&&newbusinessid.trim()!="0"){
this.formData.$tableNewId=newbusinessid;
const param = {
    body: {fid:newbusinessid},
};
this.$apiComm("${this.fromTableName}.getDataInfo",param).then(res=>{
if(res.code==0){
  this.isedit=true
for(let key in this.formData){
if(res.data[key]!=undefined){
if(typeof(res.data[key])=="string"&& ((res.data[key].indexOf("[")>=0&&res.data[key].indexOf("]")>=0)||(res.data[key].indexOf("{")>=0&&res.data[key].indexOf("}")>=0))){
try { this.formData[key]=JSON.parse(res.data[key]) } catch {
this.formData[key]=res.data[key];
}
}else{
this.formData[key]=res.data[key];
}
}
${this.loaddataaftercode}
}
}
})
}else{
  ${this.loaddataaftercode}
    this.$getNewId().then(res=>{
        if(res.code==0){
          ${this.adddataCode.join('\n')}
          this.formData.$tableNewId=res.data
        }
    })
}
}`
  this.methodsfunction['goBack_sys'] = `\n//系统方法返回\n goBack_sys() {
history.go(-1)
}`
  this.methodsfunction[
    'funmounted_sys'
  ] = `\n //系统方法页面加载方法 \n funmounted_sys(){  this.getTableNewId_sys(); ///解决手机端 文本框输入时被键盘挡住问题 \n  const clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
window.onresize = function() {
const nowClientHeight = document.documentElement.clientHeight || document.body.clientHeight;
if (clientHeight - nowClientHeight > 60 ) {//因为ios有自带的底部高度
//键盘弹出的事件处理
if(document.getElementById("maincontent")!=null){
document.getElementById("maincontent").classList.add("focusState");
}
}
else {
//键盘收起的事件处理
if(document.getElementById("maincontent")!=null){
document.getElementById("maincontent").classList.remove("focusState");
}
}
};${this.initfunstr}}`
}
BuildFrom.prototype.getcss = function() {
  return $('#commoncss').html()
}
BuildFrom.prototype.setfromData = function(options, type, title, $this) {
  switch (type) {
    case 'listview':
      this.readDataPermission[$this.model] = true
      this.writeDataPermission[$this.model] = true
      this.listviewfromData(options, title, $this)
      break
    case 'gridlist':
      this.readDataPermission[$this.model] = true
      this.writeDataPermission[$this.model] = true
      this.gridlistfromData(options, title, $this)
      var etailfrominfo = JSON.parse(JSON.stringify(this.defaultfromInfo))
      etailfrominfo.formName = title
      etailfrominfo.formCode = title
      etailfrominfo.fromTableName = options.controlId
      etailfrominfo.layoutitemlist[0].widgetForm.list[0].name = title
      etailfrominfo.layoutitemlist[0].widgetForm.list[0].columns[0].list = JSON.parse(
        JSON.stringify(options.columns)
      )
      this.griddetail[options.controlId] = etailfrominfo
    case 'upFilesComp':
      if (options.filed != undefined) {
        this.readDataPermission[$this.model] = true
        this.writeDataPermission[$this.model] = true
        this.modelfiled[$this.model] = options.filed
        if (this.formData[options.filed] == undefined) {
          this.formData[options.filed] = []
        }
      }
      break
    case 'user':
    case 'organ':
    case 'department':
    case 'role':
      if (options.filed != undefined) {
        this.readDataPermission[$this.model] = true
        this.writeDataPermission[$this.model] = true
        this.modelfiled[$this.model] = options.filed
        if (this.formData[options.filed] == undefined) {
          this.formData[options.filed] = { text: '', value: [] }
        }
      }
      break
    case 'select':
    case 'checkbox':
    case 'radio':
      if (options.filed != undefined) {
        this.readDataPermission[$this.model] = true
        this.writeDataPermission[$this.model] = true
        this.modelfiled[$this.model] = options.filed
        if (options.defaultValue != undefined) {
          this.formData[options.filed] = options.defaultValue
        } else {
          this.formData[options.filed] = ''
        }
      }
      if (options.isRemote) {
        this.buildloaddata(options, false, title)
      }
      break
    case 'autonumber':
      if (options.filed != undefined) {
        this.readDataPermission[$this.model] = true
        this.writeDataPermission[$this.model] = true
        this.modelfiled[$this.model] = options.filed
        if (options.defaultValue != undefined) {
          this.formData[options.filed] = options.defaultValue
        } else {
          this.formData[options.filed] = ''
        }
      }
      this.adddataCode.push(`this.$refs.autono${options.filed}.getmodelcode();`)
      break
    case 'datarelevance':
      if (options.filed != undefined) {
        this.readDataPermission[$this.model] = true
        this.writeDataPermission[$this.model] = true
        this.modelfiled[$this.model] = options.filed
        if (options.defaultValue != undefined) {
          this.formData[options.filed] = options.defaultValue
          this.formData[options.filed + '_text'] = ''
        } else {
          this.formData[options.filed] = ''
          this.formData[options.filed + '_text'] = ''
        }
      }
      this.builddatarelevance(options, title)
      break
    default:
      if (options.filed != undefined) {
        this.readDataPermission[$this.model] = true
        this.writeDataPermission[$this.model] = true
        this.modelfiled[$this.model] = options.filed
        if (options.defaultValue != undefined) {
          this.formData[options.filed] = options.defaultValue
        } else {
          this.formData[options.filed] = ''
        }
      }
      break
  }
  if (type !== 'listview') {
    this.rules[options.filed] = []
    if (options.ruleslist != undefined) {
      this.rules[options.filed] = JSON.parse(JSON.stringify(options.ruleslist))
    }
    if (options.required) {
      this.rules[options.filed].push({
        required: true,
        message: title + '必填',
        trigger: 'blur'
      })
    }
  }
  return ''
}
BuildFrom.prototype.setReadData = function(model) {
  this.readDataPermission[model] = true
  return ''
}
///关联列表查询控件 组装
BuildFrom.prototype.gridlistfromData = function(options, title, $this) {
  if (options.controlId != undefined) {
    if (this.formData[options.controlId] == undefined) {
      this.formData[options.controlId] = []
      //if (options.sourceApi != undefined && options.sourceApi != '' && options.sourceApi.indexOf('/') < 0) {
      this.controlData[options.controlId] = {}
      if (options.sourceApi !== undefined && options.sourceApi !== '') {
        this.controlData[options.controlId][
          'tablename'
        ] = options.sourceApi.split('.')[0]
      }
      this.controlData[options.controlId]['queryparam'] = {}
      this.controlData[options.controlId]['scopedSlots'] = []
      this.controlData[options.controlId]['pagination'] =
        options.isPage == true
          ? {
              currnet: 1,
              total: 0,
              pagesize: 10,
              isloading: false
            }
          : {}
      for (var i = 0; i < options.columns.length; i++) {
        var columnitem = options.columns[i]
        if (columnitem.isScopedSlots) {
          this.controlData[options.controlId].scopedSlots.push(
            columnitem.dataIndex
          )
        }
      }
      this.methodsfunction[
        'load' + options.controlId + '_sys'
      ] = `\n //关联列表查询控件[${title}]查询数据方法 \n load${options.controlId +
        '_sys'}(page){
    const param = {
        body: {
          ${this.WhereBuild(
            options.wherelist,
            true,
            `load${options.controlId + '_sys'}`
          )}
        }
      };
      ${
        options.isPage == true
          ? `    param.body.page=page;
      param.body.rows=this.controlData.${options.controlId}.pagination.pagesize;`
          : ''
      }
      this.controlData.${options.controlId}.pagination.isloading=true;
      this.$apiComm("${options.sourceApi}",param).then(res=>{
        this.controlData.${options.controlId}.pagination.isloading=false;
          if(res.code!=0){
              this.$message.error(res.msg);
              return;
          }
          this.formData.${options.controlId}=res.data.list;
          ${
            options.isPage == true
              ? `  this.controlData.${options.controlId}.pagination.isloading=false;
              this.controlData.${options.controlId}.pagination.total=res.totalcount;`
              : ''
          }
        });
  }`
      if (options.isPage == true) {
        this.handleTableChange(options.controlId, options.sourceApi)
      }
      this.initfunstr = this.initfunstr + `this.load${options.controlId}_sys();`
    }
  }
}
BuildFrom.prototype.handleTableChange = function(controlId, sourceApiId) {
  var handlefunname = `${controlId}handleTableChange_sys`
  var showSizefunname = `${controlId}showSizeChange_sys`
  if (this.methodsfunction[handlefunname] == undefined) {
    this.methodsfunction[
      handlefunname
    ] = `${controlId}handleTableChange_sys(pagination) {
                                          this.load${controlId}_sys(pagination)
                                      }`
  }
  if (this.methodsfunction[showSizefunname] == undefined) {
    this.methodsfunction[
      showSizefunname
    ] = `${controlId}showSizeChange_sys(page, pageSize) {
    this.controlData.${controlId}.pagination.pagesize=pageSize;
                                          this.load${controlId}_sys(1)
                                      }`
  }
  return ''
}
///生成数据关联控件
BuildFrom.prototype.builddatarelevance = function(options, title) {
  this.methodsfunction[
    'queryparam' + options.filed + '_sys'
  ] = `\n//数据关联控件[${title}]查询查询参数方法 \n queryparam${
    options.filed
  }_sys(datarow){
    const param = {
       ${this.WhereBuild(options.wherelist)}
      };
   return param;
  }`
  this.methodsfunction[
    'assign' + options.filed + '_sys'
  ] = `\n//数据关联控件[${title}]选择控件后 \n assign${
    options.filed
  }_sys(data,datarow){
    ${this.AssignBuild(options.assignFiledList)}

  }`
  this.controlData[options.filed] = options.filedList
}
///生成传入查询参数
BuildFrom.prototype.WhereBuild = function(
  wherelist,
  iswatch,
  funname,
  islistview,
  gridid
) {
  var wherecode = ''
  var watchitem = {} ////监听属性  如果明细列表设置条件过滤 有设置明细列表中的条件 那以明细列表中的字段改变 查询或者搜索 如果明细字段都是非明细字段查询的  那就要监听所有过滤条件 而过滤
  var watchdetaillist = {}
  var watchdetailfiledlist = {}
  var iswatchdetaillist = false
  wherelist.forEach(ee => {
    switch (ee.variabletype) {
      case 1:
        if (ee.value.indexOf('.') < 0) {
          wherecode = wherecode + ` ${ee.filed}:this.formData.${ee.value},`
        } else {
          let filed = ee.value.split('.')
          wherecode =
            wherecode +
            ` ${ee.filed}:datarow!==undefined? datarow.${filed[1]}:undefined,`
        }
        if (iswatch) {
          if (ee.value.indexOf('.') < 0) {
            if (watchitem[`formData.${ee.value}`] === undefined) {
              watchitem[`formData.${ee.value}`] = []
            }
            watchitem[`formData.${ee.value}`].push(`this.${funname}();`)
          } else {
            iswatchdetaillist = true
            let filed = ee.value.split('.')
            this.watchdetailfiled[filed[1]] = ''
            if (watchdetaillist[`formData.${filed[0]}`] === undefined) {
              watchdetaillist[`formData.${filed[0]}`] = []
            }
            watchdetaillist[`formData.${filed[0]}`].push(
              `  this.watchdetailfiled.${filed[1]}=newVal[this.selectindex].${filed[1]};`
            )
            if (
              watchdetailfiledlist[`watchdetailfiled.${filed[1]}`] === undefined
            ) {
              watchdetailfiledlist[`watchdetailfiled.${filed[1]}`] = []
            }
            watchdetailfiledlist[`watchdetailfiled.${filed[1]}`].push(
              `   this.${funname}(this.formData.${filed[0]}[this.selectindex]);`
            )
          }
        }
        break
      case 2:
        wherecode = wherecode + ` ${ee.filed}:'${ee.value.replace("'", '"')}',`
        break
      case 3:
        wherecode =
          wherecode +
          ` ${ee.filed}: this.$query('${ee.value.replace("'", '"')}'),`
        break
      case 4:
        wherecode = wherecode + ` ${ee.filed}:this.${ee.value},`
        break
    }
  })
  if (iswatchdetaillist === true) {
    watchitem = {}
  }

  if (islistview === true && JSON.stringify(watchdetaillist) === '{}') {
    watchdetaillist[`formData.${gridid}`] = []
    watchdetaillist[`formData.${gridid}`].push(
      `  this.${funname}(newVal[this.selectindex]);`
    )
  }
  for (var key in watchitem) {
    this.watchfunction[key] = `'${key}'(){$$##$$}`
    if (this.watchfunctionContext[key] !== undefined) {
      this.watchfunctionContext[key] = this.watchfunctionContext[key].concat(
        watchitem[key]
      )
    } else {
      this.watchfunctionContext[key] = watchitem[key]
    }
  }
  for (var key in watchdetaillist) {
    this.watchfunction[key] = `'${key}':{
      deep:true,
      handler:function(newVal){
        if(this.selectproperty!==undefined&&this.selectindex!==undefined){
        if(newVal[this.selectindex]!==undefined&&newVal[this.selectindex][this.selectproperty]!==undefined){$$##$$}
        }
      }
    }`
    if (this.watchfunctionContext[key] !== undefined) {
      this.watchfunctionContext[key] = this.watchfunctionContext[key].concat(
        watchdetaillist[key]
      )
    } else {
      this.watchfunctionContext[key] = watchdetaillist[key]
    }
  }
  for (var key in watchdetailfiledlist) {
    this.watchfunction[key] = `'${key}'(){$$##$$}`
    if (this.watchfunctionContext[key] !== undefined) {
      this.watchfunctionContext[key] = this.watchfunctionContext[key].concat(
        watchdetailfiledlist[key]
      )
    } else {
      this.watchfunctionContext[key] = watchdetailfiledlist[key]
    }
  }
  if (wherecode.length > 0) {
    return wherecode.substring(0, wherecode.length - 1)
  } else {
    return ''
  }
}
///生成赋值规则函数
BuildFrom.prototype.AssignBuild = function(assignlist) {
  var assigncode = ''
  assignlist.forEach(ee => {
    if (ee.value.indexOf('.') < 0) {
      assigncode =
        assigncode +
        `this.formData.${ee.value}=data===undefined?undefined:data.${ee.filed};`
    } else {
      let filed = ee.value.split('.')
      assigncode =
        assigncode +
        `datarow.${filed[1]}=data===undefined?undefined:data.${ee.filed};`
    }
  })
  return assigncode
}
///生成查询关联控件
BuildFrom.prototype.buildqueryrelevance = function(element, islistview) {
  this.methodsfunction['load' + element.model + '_sys'] = `\n //查询关联控件[${
    element.title
  }]查询数据方法 \n load${element.model + '_sys'}(datarow){
    const param = {
        body: {
          ${this.WhereBuild(
            element.options.wherelist,
            true,
            `load${element.model + '_sys'}`,
            islistview,
            element.gridid
          )}
        }
      };
      this.$apiComm("${element.options.sourceApi}",param).then(res=>{
          if(res.code!=0){
              this.$message.error(res.msg);
              return;
          }
          if(res.data.list.length>0){
              this.assign${element.model}_sys(res.data.list[0],datarow)
          }else{
              this.assign${element.model}_sys(undefined,datarow)
          }
        });
  }`
  this.methodsfunction['assign' + element.model + '_sys'] = `\n//查询关联控件[${
    element.title
  }]查询数据后赋值方法 \n assign${element.model + '_sys'}(data,datarow){
    ${this.AssignBuild(element.options.assignFiledList)}
  }`
  ////如果没有监控明细列
  if (islistview != true) {
    this.initfunstr = this.initfunstr + `this.load${element.model}_sys();`
  }
  return ''
}
BuildFrom.prototype.buildloaddata = function(options, islistview, title) {
  this.methodsfunction[
    'load' + options.filed + '_sys'
  ] = `\n ///【${title}】加载选项方法\n load${options.filed}_sys(datarow){
    const param = {
      body: {${this.WhereBuild(
        options.wherelist,
        true,
        `load${options.filed + '_sys'}`
      )}},
      };
      this.$apiComm("${options.sourceApi}",param).then(res=>{
        if(res.code!=0){
        this.$message.error(res.msg);
        return;
        }
        this.controlData.${options.filed}=res.data.list
        });
  }`
  if (islistview !== true) {
    this.initfunstr = this.initfunstr + `this.load${options.filed}_sys();`
  }

  this.controlData[options.filed] = []
}
///生成下拉列表，单选，多选，关联数据，查询数据的动态数据绑定代码 入口
BuildFrom.prototype.setlistviewfromItemData = function(columnitem, $this) {
  switch (columnitem.type) {
    case 'select':
    case 'checkbox':
    case 'radio':
      ///生成单选多选方法
      if (columnitem.options.isRemote) {
        this.buildloaddata(columnitem.options, true, columnitem.title)
      }
      break
    case 'datarelevance':
      this.builddatarelevance(columnitem.options, columnitem.title)
      break
    case 'queryrelevance':
      this.buildqueryrelevance(columnitem, true)
      break
    default:
      break
  }
}

///子表fromdata 组装
BuildFrom.prototype.listviewfromData = function(options, title, $this) {
  if (options.controlId != undefined) {
    if (this.formData[options.controlId] == undefined) {
      this.formData[options.controlId] = []
      this.formData['del' + options.controlId] = []
      //if (options.sourceApi != undefined && options.sourceApi != '' && options.sourceApi.indexOf('/') < 0) {
      this.controlData[options.controlId] = {}
      this.controlData[options.controlId]['queryparam'] = {}
      this.controlData[options.controlId]['scopedSlots'] = []
      this.controlData[options.controlId]['pagination'] = {}
      for (var i = 0; i < options.columns.length; i++) {
        var columnitem = options.columns[i]
        if (columnitem.isScopedSlots) {
          this.controlData[options.controlId].scopedSlots.push(
            columnitem.dataIndex
          )
          if (columnitem.type === 'datarelevance') {
            this.controlData[options.controlId].scopedSlots.push(
              columnitem.dataIndex + '_text'
            )
          }
        }
        if (
          columnitem.options != null &&
          columnitem.options.filed != '' &&
          columnitem.options.filed != undefined
        ) {
          this.readDataPermission[columnitem.model] = true
          this.writeDataPermission[columnitem.model] = true
          this.modelfiled[columnitem.model] = columnitem.options.filed
          this.rules[columnitem.options.filed] = []
          if (columnitem.options.ruleslist != undefined) {
            this.rules[columnitem.options.filed] = JSON.parse(
              JSON.stringify(columnitem.options.ruleslist)
            )
          }
          if (columnitem.options.required == true) {
            this.rules[columnitem.options.filed].push({
              required: true,
              message: columnitem.title + '必填',
              trigger: 'blur'
            })
          }
        }
        this.setlistviewfromItemData(columnitem, $this)
      }
      this.methodsfunction[
        'load' + options.controlId + '_sys'
      ] = `\n//子表[${title}]数据加载事件 \n load${options.controlId +
        '_sys'}(){
      if(this.formData.$tableNewId!=null&&this.formData.$tableNewId!=""){
      const param = {
      body: {$tableNewId:this.formData.$tableNewId},
      };
      this.$apiComm("${options.controlId}.querylist",param).then(res=>{
      if(res.code!=0){
      this.$message.error(res.msg);
      return;
      }
      this.formData.${options.controlId}=res.data.list
      });
      }
      }`
      this.methodsfunction[
        options.controlId + 'rowclick_sys'
      ] = `\n//子表[${title}]单机行事件 \n ${options.controlId}rowclick_sys(rows,column,event){ this.selectproperty=column===undefined?undefined:column.property; this.selectindex=this.formData.${options.controlId}.indexOf(rows);} `
      this.initfunstr = this.initfunstr + `this.load${options.controlId}_sys();`
    }
  }
}
//生成事件方法
BuildFrom.prototype.buildEvent = function(element) {
  var eventcode = ''
  if (element.eventlist == undefined) {
    return eventcode
  }
  for (var i = 0; i < element.eventlist.length; i++) {
    if (
      element.eventlist[i].value != '' &&
      element.eventlist[i].value != undefined
    ) {
      eventcode =
        eventcode +
        ' @' +
        element.eventlist[i].label +
        '=' +
        element.eventlist[i].value +
        ''
      if (element.eventlist[i].value.indexOf('(') < 0) {
        if (this.methodsfunction[element.eventlist[i].value] == undefined) {
          this.methodsfunction[element.eventlist[i].value] =
            '\n // [' +
            element.title +
            element.eventlist[i].label +
            ']事件方法 \n' +
            element.eventlist[i].value +
            '() { }'
        } else {
          this.methodsfunction[funname] =
            '\n // [' +
            element.title +
            element.eventlist[i].label +
            ']事件方法 \n' +
            methodsfunction[funname]
        }
      } else {
        var funname = element.eventlist[i].value
        funname = funname.substring(0, element.eventlist[i].value.indexOf('('))
        if (this.methodsfunction[funname] == undefined) {
          var funparamstr = element.eventlist[i].value
            .replace(funname + '(', '')
            .replace(')', '')
            .split(',')
          var funname = funname + '('
          if (funparamstr[0] != '') {
            for (var i = 0; i < funparamstr.length; i++) {
              if (i == 0) {
                funname = funname + 'param' + i
              } else {
                funname = funname + ',param' + i
              }
            }
          }
          funname = funname + ')'
          this.methodsfunction[funname] =
            '\n // [' +
            element.title +
            element.eventlist[i].label +
            ']事件方法 \n' +
            funname +
            '{ }'
        } else {
          this.methodsfunction[funname] =
            '\n // [' +
            element.title +
            element.eventlist[i].label +
            ']事件方法 \n' +
            this.methodsfunction[funname]
        }
      }
    }
  }
  return eventcode
}

BuildFrom.prototype.getrowcontrolcss = function(element) {
  var cssstr = 'padding:0px'
  cssstr =
    cssstr +
    (this.minheight.indexOf(element.type + ',') == 0 ? 'min-height:42px' : '')
  cssstr =
    cssstr + this.dynamicheight.indexOf(element.type + ',') >= 0
      ? 'height:auto'
      : element.options.rowSpan !== undefined
      ? 'height' + element.options.rowSpan * 42 + 'px'
      : 'height:auto'
  return cssstr
}
